<?php
//模块名_控制名
class Customer_BalanceController extends Zend_Controller_Action
{

	public function preDispatch(){
		$this->tplDirectory = "customer/balance/";	//控制对应模板目录
	}
	
	public function indexAction(){
		$bank=Bank_Service_Account::getByCondition(array('ba_status'=>1));
		$currency=Common_Service_Currency::getByCondition(array());
		$currencyarr=array();
		foreach($currency as $key => $value){
			$currencyarr[$value['currency_id']]=$value['currency_rate'];
		}
		$this->view->jsonAllCurrency=json_encode($currencyarr);
		$this->view->currency=$currency;
		$this->view->bank=$bank;
		echo Cff::renderTpl($this->tplDirectory."index.tpl","layout");	
    }
	
	/**
	*根据客户帐号返回相关资料
	*@param customer_id
	*return json
	*/
    public function singleAction(){
    	$customer_id = $this->_request->getParam('customer_id','');
    	if($customer_id){
    		$result = Customer_Service_Customer::getByCustomerId($customer_id);
    		if( count($result) ){
				$balance = Customer_Service_Balance::getByCustomerId($customer_id);
				if(!empty($balance)){
					$currencyRows=Common_Service_Currency::getByValue($balance['currency_code'],'currency_code');
					$result['cb_value']=$balance['cb_value'];
					$result['currency_code']=$balance['currency_code'];
					$result['cb_hold_value']=$balance['cb_hold_value'];
					$result['currency_id']=$currencyRows['currency_id'];
				}
				
				$data=array(
					'result'=>$result,
					'ask'=>1
				);
			}else{
				$data=array(
					'result'=>$result,
					'ask'=>0
				);
			}
			die(Zend_Json::encode($data));
    	}
    }
    
	/**
	*根据银行帐号返回对应币种
	*@param ba_id
	*return json
	*/
    public function getaccountmapAction(){
    	$ba_id = $this->_request->getParam('ba_id','');
    	if($ba_id){
    		$result = Bank_Service_AccountMap::getByCondition(array('ba_id'=>$ba_id));
    		if( count($result) ){
				$currencyresult=array();
				foreach($result as $key => $value){
					$Currency=Common_Service_Currency::getByValue($value['currency_code'],'currency_code');
					if(!empty($Currency)){
						$currencyresult[$key]['currency_cn_name']=$Currency['currency_cn_name'];
						$currencyresult[$key]['currency_id']=$Currency['currency_id'];
					}
				}
				$data=array(
					'result'=>$currencyresult,
					'ask'=>1
				);
			}else{
				$data=array(
					'error'=>array('操作失败'),
					'ask'=>0
				);
			}
			die(Zend_Json::encode($data));
    	}
    }
	
	/**
	*根据帐号及币种返回对应手续费
	*@param ba_id currency_id
	*return json
	*/
    public function getfeerateAction(){
    	$ba_id = $this->_request->getParam('ba_id','');
    	$currency_id = $this->_request->getParam('currency_id','');
		$data=array(
			'ask'=>0
		);
    	if($ba_id && $currency_id){
			$currency=Common_Service_Currency::get($currency_id);
			if(!empty($currency)){
				$result = Bank_Service_AccountMap::getByRate(array('ba_id'=>$ba_id,'currency_code'=>$currency['currency_code']));
			}
    		if( count($result) ){
				$data=array(
					'result'=>$result,
					'ask'=>1
				);
			}else{
				$data=array(
					'error'=>array('操作失败'),
					'ask'=>0
				);
			}
    	}
		die(Zend_Json::encode($data));
    }
	
	/**
	*根据用户ID返回用户帐户历史记录
	*@param customer_id
	*return json
	*/
	public function historylistAction(){
		$intpage=$this->_request->getParam('page',1);
		$pageSize=$this->_request->getParam('pageSize');
		$customer_id=$this->_request->getParam('customer_id');
        if($customer_id==''){
			$return=array(
				'ask'=>0,
				'error'=>array('操作失败!'),
			);
            die(Zend_Json::encode($return));
        }
		$intpage = ($intpage<1)?1:$intpage;
		$condition=array(
			'customer_id'=>$customer_id,
		);
		$count=Customer_Service_BalanceHistory::getByCondition($condition,'count(*)','',0,0);
		$data=array(
			'count'=>0,
			'ask'=>0
			);
		if($count>0){
			$Rows=Customer_Service_BalanceHistory::getByCondition($condition,'*',array('cbh_id desc'),$pageSize,$intpage);
			foreach($Rows as $key => $value){
				$Urow=User_Service_User::getByUserId($value['user_id']);
				if($Urow){
					$Rows[$key]['user_id']=$Urow['name'];
				}
			}
			$data=array(
				'count'=>$count,
				'result'=>$Rows,
				'ask'=>1
			);
		}
		die(Zend_Json::encode($data));
	}
	
	/**
	*根据用户ID充值
	*@param customer_id,options
	*return json
	*/
	public function rechargeAction(){
	
        $dn_id = $this->_request->getPost('dn_id');
		$type = $this->_request->getPost('type');									//入款OR扣款
		$CustomerId = $this->_request->getPost('CustomerId');							//客户ID
		$arriveCurrency = $this->_request->getPost('arriveCurrency');				//到帐币种ID
		$arriveMoney = $this->_request->getPost('arriveMoney');						//到帐金额
		$arriveCurrencyRate = $this->_request->getPost('arriveCurrencyRate');		//到账币种对人民币的汇率
		$rate = trim($this->_request->getPost('rate'));								//到账币种对客户币种的汇率
		$userCurrencyId = $this->_request->getPost('userCurrencyId');				//客户币种ID
		$money = $this->_request->getPost('money');									//实际金额
		$baId = $this->_request->getPost('baId');									//银行账号ID
		$udoId = $this->_request->getPost('udoId');									//交易号
		$reason = trim($this->_request->getPost('reason'));							//备注
		$arriveTime = $this->_request->getPost('arriveTime');
		$arriveTime = $arriveTime == '' ? date('Y-m-d H:i:s'):$arriveTime;			//到账日期
		$doFee = $this->_request->getPost('doFee');									//是否扣手续费
		$feeRate = $this->_request->getPost('feeRate');								//手续费率
		$session = new Zend_Session_Namespace('userAuth');                          
		$user_id=$session->userID;													//操作人

		//表单验证
        $error = array();
        $validateArr = array();
        $validateArr[] = array('name' => '客户ID', 'value' => $CustomerId, 'regex' => array('require', 'length[0,255]', ));
		$validateArr[] = array('name' => '到帐币种', 'value' => $arriveCurrency, 'regex' => array('require', 'length[0,255]', ));
		$validateArr[] = array('name' => '银行账号', 'value' => $baId, 'regex' => array('require',));
		$validateArr[] = array('name' => '到帐金额', 'value' => $arriveMoney, 'regex' => array('require','positive1',));
        $error = Common_Service_Validator::formValidator($validateArr);
        if(!empty($error)){
			$return=array(
				'ask'=>0,
				'error'=>$error,
			);
            die(Zend_Json::encode($return));
        }
		
		$currency=Common_Service_Currency::get($arriveCurrency);
		$currencyCode=$currency['currency_code'];									//币种CODE

		$options=array(
			'cbhClass'=>0,
			'refCode'=>$udoId,
			'appType'=>1,//目前只做充值
			'cbhType'=>1,//目前只做充值
			'vale'=>$arriveMoney, //没转换的金额
			'currencyCode'=>$currencyCode,
			'cbharrive'=>$arriveTime,  //到帐日期
			'rate'=>$rate,
			'note'=>$reason,
			'userId'=>$user_id,
		);
		$result=Customer_Service_CustomerBalance::procBalanceTransaction($CustomerId,$options);
		if($result){
			$data=array(
				'result'=>$result,
				'ask'=>1
			);
		}else{
			$data=array(
				'ask'=>0,
				'error'=>array('操作失败!'),
			);
		}
		
		die(Zend_Json::encode($data));
	
	}
	
}